import{
    LOGIN_OK,
    GET_USER_INFO,
    GET_GOOD_LIST,
    GET_CATE_LIST,
    UPDATE_GOOD,
    GET_GOOD_INFO,
    CLEAR_CACHE,
    USER_LIST
} from '../actionType'

import { fetchLogin, fetchUserInfo, fetchGoodList, fetchAllCates,fetchGoodUpdate,
    fetchGoodInfo, fetchUserList } from '@/api'

//在redux中，默认不支持异步的action
export function login(payload,callback){
    return dispatch=>{
        fetchLogin(payload).then(res=>{
            console.log(res);
            if(res){
                localStorage.setItem('token', res.token)
                dispatch({
                    type: LOGIN_OK,
                    payload: res.token
                })
                //跳转进入系统内部
                callback&&callback()

            }
        })
    }

}

//获取用户信息
export function  getinfo() {
    return dispatch=>{
        fetchUserInfo().then(res=>{
            console.log('userinfo',res)
            //把用户信息放到redux中
            dispatch({
                type: GET_USER_INFO,
                payload:res.userInfo
            })
        })
    }
}
//获取用户列表
export function getUserList() {
    return dispatch => {
        fetchUserList().then(res => {
            console.log('userList', res)
            //把用户信息放到redux中
            dispatch({
                type: USER_LIST,
                payload: res
            })
        })
    }
}



//获取商品列表
export function getGoodList(payload) {
    return dispatch=>{
        fetchGoodList(payload).then(res=>{
            console.log(res);
            
            dispatch({
                type:GET_GOOD_LIST,
                payload:res
            })
        })
    }
}
//获取种类列表
export function getCatesList(payload) {
    return dispatch=>{
        fetchAllCates(payload).then(res=>{
            console.log(res);
            
            dispatch({
                type:GET_CATE_LIST,
                payload:res
            })
        })
    }
}
//获取种类列表
export function getGoodUpdate(payload) {
    return dispatch=>{
        fetchGoodUpdate(payload).then(res=>{
            console.log(res);
            
            dispatch({
                type: UPDATE_GOOD,
                payload:''
            })
        })
    }
}
//获取种类列表
export function getGoodInfo(payload) {
    return dispatch=>{
        fetchGoodInfo(payload).then(res=>{
            console.log(res);
            
            dispatch({
                type: GET_GOOD_INFO,
                payload:res.info
            })
        })
    }
}
//清除缓存
export function clearGoodCache() {
    return{
        type: CLEAR_CACHE
    }
}